# -*- coding: utf-8 -*-
import re

import requests

baidu = 'http://www.baidu.com/'
httpbin = 'https://www.httpbin.org'
scrape = 'https://ssr1.scrape.center/'
ssr3 = 'https://ssr3.scrape.center/'
headers = {
    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36'
}


def sample_demo(url=baidu):
    r = requests.get(url)
    # print(response.text.encode('gbk', 'replace'))
    print(type(r))
    print(r.status_code)
    print(type(r.text))
    print(r.text[:100])
    print(r.cookies)


def request_with_proxy():
    proxies = {
        'http': 'http://127.0.0.1:8888',
        'https': 'https://127.0.0.1:8888'
    }
    r = requests.get(httpbin, proxies=proxies)
    print(r.text)


def request_method(mthod='get'):
    url = f'https://www.httpbin.org/{mthod}'
    # r = requests.get(url)
    r = getattr(requests, mthod)(url)
    print(r.text)


def request_with_args(data=None):
    url = httpbin + '/get'
    if data is None:
        data = {'name': 'vincent', 'age': 20}
    r = requests.get(url, params=data)

    print(r.text)


def get_titles():
    r = requests.get('https://ssr1.scrape.center/')
    pattern = re.compile('<h2.*?>(.*?)</h2>', re.S)
    titles = re.findall(pattern, r.text)
    print(titles)


def get_bytes():
    r = requests.get('https://github.com/favicon.ico')
    with open('favicon.ico', 'wb') as f:
        f.write(r.content)
    print(r.status_code)
    print(r.headers['content-type'])
    # print(r.encoding)
    # print(r.apparent_encoding)
    # print(r.text)
    print(r.content)


def request_with_headers():
    r = requests.get(scrape, headers=headers)
    # print(r.text)
    print(type(r.status_code), r.status_code)
    print(type(r.headers), r.headers)
    print(type(r.cookies), r.cookies)
    print(type(r.url), r.url)
    print(type(r.history), r.history)


def request_upload():
    files = {'icon': open('favicon.ico', 'rb')}
    r = requests.post(httpbin + '/post', files=files)
    print(r.text)


def request_cookies():
    """
    url = httpbin + '/cookies'
    cookies = dict(cookies_are='working')
    r = requests.get(url, cookies=cookies)
    print(r.text)
    """
    r = requests.get(baidu)
    print(r.cookies)
    for key, value in r.cookies.items():
        print(key + '=' + value)


def requet_with_cookie():
    _headers = {
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36',
        'Cookie': '__Host-user_session_same_site=rCxLHrVzF-mEVVUS0f7Opj6hvpqvi519XYRmf3P8d9J_ERtC;_device_id=d61edd1d6abf42a9b3aa7b2c3367f787;_gh_sess=t0%2Famst%2FV%2BDByZFzSKrVnNYononQKGM1UmIPNS16iUxkqpHsqTmSypQ10oZA69WFztjZ%2FKAFelzyJ5%2F43VCQKzbobVFqbWjYp8l1JaLr3mNYpd1N%2BNYSKWy2BV7Cf4OftMz9fngTzUdagbiZMo8Xm1omOYYF61lEJZqGS7LsRpAt8CiGYbXyFGoTI4T3oi5taQD2ThkQUEOnGLmR1%2BN0uAzoooM4Nw1Q2mnhl7Htvxhi5vYCs7%2BKkGmZGgMlNWx52j2%2FNuPcS%2B5Y5aHYwJztm467i%2BHaiO3HGp3mUQ%2BjywHcPM5BGTiZSK9o2U6bVN4xDe1ORwRzAVwlmJnGXkmdm0oqH7yz4ckU3dmaCXbEg0X7Sku1sc%2FICqIEZMnPDe9XonMU6W63yf1eW4LjlgFgpY%2FHQPMdcKWNNC9pOu8TLXUDzDd29euDTGoeRR5rg5aHPXcHtwbvFYm0i7kccCqXxNBwPHrPEBgfoMK1NF%2BqWv7dd0d9Z21%2BbEFGlojLxL%2BAsfG67AmurFKl14CJN5gunhpxVcuwFYkTnV%2FjRUnzukEj2rfQhNy%2BiveiAHp47ZLoSR7S8GN9wRGG89l39T0CVz67k5nP7e9bO3nEoTDmxABPsKg%2FmLDAzD%2FbvfT9SjE6gcyZKYHI%2BzQFO%2BZS9tyox3zzvwG42mU8qFcK3lllwzu8qNzWY44%2B8Q%2BkpahOFsPOZ%2BWK5VaYb2uuewVErekUw%2Fp71GdjRuy%2BD8iGnN4%2FeHDO%2BklkOA6CGh2XLAMIIQGA3IOtsiOXo5v5ZJo%2BKdDMnT5UIW2rmJGgqLZVgF9pqXsXxDCBJSSYbvCHeJYeqqi%2BJwE6BlnQinLIf%2BpMZra%2B7DQWhx2OFv1X9Hy4XsH8XgETQaEGw67xGWDb6Vn4UQk04GI0gbV6%2FTJ9tjQ4qpM6cQoqkpzym5egeU2rOc%2B2MiYYOY4LoUWXdUdhWeDHHEo4slKXntVM9h%2BixjNQ--pC6IQFuG47%2FBu9RS--y0PJlChM5qhIsTl6o4OSuA%3D%3D;_locale=en;_octo=GH1.1.920329916.1714900422;dotcom_user=touchX;has_recent_activity=1;logged_in=yes;saved_user_sessions=960433%3ArCxLHrVzF-mEVVUS0f7Opj6hvpqvi519XYRmf3P8d9J_ERtC;user_session=rCxLHrVzF-mEVVUS0f7Opj6hvpqvi519XYRmf3P8d9J_ERtC'
    }
    r = requests.get('https://github.com/', headers=_headers)
    encoding = r.encoding if r.encoding else 'utf-8'
    content = r.content.decode(encoding)
    print(content)


def request_with_session():
    requests.get('http://httpbin.org/cookies/set/sessioncookie/aaaaa')
    r = requests.get('http://httpbin.org/cookies')
    print(r.text)

    s = requests.Session()
    s.get('http://httpbin.org/cookies/set/sessioncookie/123456789')
    r = s.get('http://httpbin.org/cookies')
    print(r.text)


def request_with_ssl():
    r = requests.get('https://ssr2.scrape.center/', verify=False)
    print(r.status_code)


def request_with_auth():
    r = requests.get(ssr3, auth=('admin', 'admin'))
    print(r.status_code)


def prepare_request():
    url = httpbin + '/post'
    data = {'name': 'vincent', 'age': 20}
    s = requests.Session()
    req = requests.Request('POST', url, data=data, headers=headers)
    prepped = s.prepare_request(req)
    r = s.send(prepped)
    print(r.text)


if __name__ == '__main__':
    prepare_request()
    # request_with_proxy()
    # request_with_auth()
    # request_with_ssl()
    # sample_demo()
    # request_method(mthod='post')
    # request_method(mthod='patch')
    # request_with_args()
    # get_titles()
    # get_bytes()
    # request_with_headers()
    # request_upload()
    # request_cookies()
    # requet_with_cookie()
    # request_with_session()
